from rest_framework import serializers

from apps.article.models import Article
from apps.question.models import Label, Question, Reply
from apps.user.models import User


class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = "__all__"


class ArticleSerializerSimple(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ("id", "title")


class UserDetailSerializer(serializers.ModelSerializer):
    articles = ArticleSerializerSimple(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class GetAllLableSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ('id', 'label_name')


class ArticleSerializerForList(serializers.ModelSerializer):
    user = UserDetailSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title", "content", "createtime", "user", "collected_users", "collected", "image", "visits")


class QuestionSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = LabelSerializer(read_only=True, many=True)

    class Meta:
        model = Question
        fields = '__all__'


class LabelSerializerWithQuestionAndArticle(serializers.ModelSerializer):
    questions = QuestionSerializer(many=True, read_only=True)
    articles = ArticleSerializerForList(many=True, read_only=True)

    class Meta:
        model = Label
        fields = "__all__"


class UserSerializerSimple(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("id", 'username', 'avatar')


class ReplySerializerForSubAndParent(serializers.ModelSerializer):
    user = UserSerializerSimple(read_only=True)

    class Meta:
        model = Reply
        fields = ("id", "user", "content", "createtime", "useful_count", "unuseful_count")


class ReplySerializerForList(serializers.ModelSerializer):
    user = UserSerializerSimple(read_only=True)
    parent = ReplySerializerForSubAndParent(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)

    class Meta:
        model = Reply
        fields = ("id", "user", "subs", "parent", "content", "createtime", "useful_count", 'problem', "unuseful_count")


class QuestionSerializerForCreate(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = '__all__'


class QuestionSerializerForDetail(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    comment_question = ReplySerializerForList(read_only=True, many=True)
    answer_question = ReplySerializerForList(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ("id", "createtime", "labels", "reply", "replyname", "replytime", "title", "unuseful_count",
                  "useful_count", "user", "visits", "content", "comment_question", "answer_question")


class ReplySerializerForCreate(serializers.ModelSerializer):
    class Meta:
        model = Reply
        fields = '__all__'

    def create(self, validated_data):
        reply = Reply.objects.create(**validated_data)
        question = reply.problem
        if validated_data['type'] == 2:
            question.reply += 1
            question.replytime = reply.createtime
            question.replyname = reply.user.username
        return reply


class QuestionSerializerForLabel(serializers.ModelSerializer):
    id = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Question
        exclude = ('content', 'thumbup', 'solve', 'updatetime')


class LabelSerializerForDetail(serializers.ModelSerializer):
    id = serializers.PrimaryKeyRelatedField(read_only=True)
    questions = QuestionSerializerForLabel(read_only=True, many=True)
    user_id = serializers.IntegerField(read_only=True)

    class Meta:
        model = Label
        fields = '__all__'
